package com.moyuwanjia_anno.Dao.Impl;

import com.moyuwanjia_anno.Dao.IAccountDao;
import com.moyuwanjia_anno.entity.Account02_anno;
import com.moyuwanjia_anno.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author 摸鱼丸加~
 * @create 2022-04-24 14:38
 */
@Repository(value = "accountDao")
public class AccountDaoImpl implements IAccountDao {

    private QueryRunner queryRunner = new QueryRunner();
    @Autowired
    private ConnectionUtils connectionUtils;

    public List<Account02_anno> findAll() {
        try {
            return queryRunner.query(connectionUtils.getThreadConnection(),"select * from account02",
                    new BeanListHandler<Account02_anno>(Account02_anno.class));
        }catch (Exception exception){
            throw new RuntimeException(exception);
        }
    }

    public Account02_anno findAccountById(Integer id) {
        try {
            return queryRunner.query(connectionUtils.getThreadConnection(),"select * from account02 where id = ?",new BeanHandler<Account02_anno>(Account02_anno.class),id);
        }catch (Exception exception){
             throw new RuntimeException(exception);
        }
    }

    public Integer saveAccount(Account02_anno account02Anno) {
        try {

            return queryRunner.update(connectionUtils.getThreadConnection(),"insert into account02(name,money) values(?,?)", account02Anno.getName(), account02Anno.getMoney());
        } catch (SQLException sqlException) {
            throw  new RuntimeException(sqlException);
        }
    }

    public Integer updateAccount(Account02_anno account02Anno) {
        try {
            return queryRunner.update(connectionUtils.getThreadConnection(),"update account02 set money=? where name = ?", account02Anno.getMoney(), account02Anno.getName());
        } catch (SQLException sqlException) {
            throw new RuntimeException(sqlException);
        }
    }

    public Integer deleteAccountById(Integer id) {
        try {
            return queryRunner.update(connectionUtils.getThreadConnection(),"delete from account02 where id = ?",id);
        } catch (SQLException sqlException) {
            throw new RuntimeException(sqlException);
        }
    }

    /**
     * 根据名称查询对应数据，只能返回一条数据
     * @param accountName
     * @return
     */
    @Override
    public Account02_anno findAccountByName(String accountName) {
        try {
            final List<Account02_anno> query = queryRunner.query(connectionUtils.getThreadConnection(),"select * from account02 where name = ?",
                    new BeanListHandler<Account02_anno>(Account02_anno.class),
                    accountName);
            if(CollectionUtils.isEmpty(query) || query.size()==0) return null;
            if(query.size() > 1) throw new RuntimeException("数据存在问题，结果集不唯一");
            return query.get(0);
        }catch (SQLException exception){
           throw new RuntimeException(exception);
        }
    }
}
